package com.fuerda.izb.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.fuerda.izb.R
import com.fuerda.izb.utils.dp2px

class SpeedLineView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val minF=9f   //    取1/9 的 9
    private val maxF=5f   //      取5/9 的 5
    private val lineCount = 22 // 要绘制的竖线数量

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = context.getColor(R.color.speed_line_color)
        style = Paint.Style.FILL
        strokeWidth = dp2px(context, 3).toFloat() // 设置线条宽度
        strokeCap = Paint.Cap.ROUND // 设置线条端点为圆形
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val minHeight = height / minF
        val maxHeight = height * maxF / minF
        val step = (maxHeight - minHeight) / (lineCount.toFloat())
        for (i in 0..21) {
            val v1 = top + (bottom - top) / 2f
            val v2 = ((height / minF) + step * i) / 2f
            val lineTop = v1 - v2
            val lineBottom = v1 + v2
            val start =
                0 + (i + 1) * (((width - (lineCount * (paint.strokeWidth))) / (lineCount + 1))) + paint.strokeWidth * i  //不是从left开始，而是0,  间距=（总长-竖线总长）/（竖线总数+1）
            canvas.drawLine(start, lineTop, start, lineBottom, paint)
        }
    }
}